#pragma once

#include <ddm/AtomicRoute.h>
#include <folly/IPAddress.h>
#include <openr/common/NetworkUtil.h>
#include <openr/if/gen-cpp2/KvStore_types.h>

#include <string>

namespace ddm {
class Util {
 public:
  static void dumpKeyVals(const openr::thrift::KeyVals& keyVals);
};

inline std::pair<long, long> computeRange(folly::CIDRNetwork prefix) {
  long low = prefix.first.asV4().mask(prefix.second).toLongHBO();
  long high = low + (1 << (32 - prefix.second));
  return std::make_pair(low, high);
}

inline std::pair<long, long> computeRange(std::string prefix, int prefixLen) {
  folly::CIDRNetwork hs = folly::IPAddress::createNetwork(prefix, prefixLen);
  return computeRange(hs);
}

inline RANGES intersection(RANGES ranges1, RANGES ranges2) {
  RANGES intersection;
  for (auto& r1 : ranges1) {
    for (auto& r2 : ranges2) {
      long lb = std::max(r1.first, r2.first);
      long ub = std::min(r1.second, r2.second);
      if (ub > lb) {
        intersection.push_back(std::make_pair(lb, ub));
      }
    }
  }
  return intersection;
}

}  // namespace ddm